//
//  DLCircleListViewModel.m
//  FRP_OC
//
//  Created by DeLongYang on 2017/11/3.
//  Copyright © 2017年 DeLongYang. All rights reserved.
//

#import "DLCircleListViewModel.h"
#import "DLCircleListCollectionCellViewModel.h"   // cellMode

@interface DLCircleListViewModel()

/**
 当前的 page
 */
@property (nonatomic,assign)NSInteger currentPage;

@end

@implementation DLCircleListViewModel

#pragma mark ----  DLView Protocol
- (void)dl_initialize
{
    // 模拟接收到 网络请求的数据 然后加上我们自定义的 假数据
    @weakify(self)
    [self.refreshDataCommand.executionSignals.switchToLatest subscribeNext:^(NSDictionary  *x) {
        //
        @strongify(self)
        NSMutableArray *alArray = [NSMutableArray array];
        for (int i=0; i<8; i++) {
            DLCircleListCollectionCellViewModel *viewModel = [[DLCircleListCollectionCellViewModel alloc] init];
            viewModel.headerImageStr = @"http://mmbiz.qpic.cn/mmbiz/XxE4icZUMxeFjluqQcibibdvEfUyYBgrQ3k7kdSMEB3vRwvjGecrPUPpHW0qZS21NFdOASOajiawm6vfKEZoyFoUVQ/640?wx_fmt=jpeg&wxfrom=5";
            viewModel.name = @"我是一只鹦鹉鱼";
            [alArray addObject:viewModel];
        }
        
        self.listHeaderViewModel.dataArray = alArray;
        
        NSMutableArray *reArray = [NSMutableArray array];
        for (int i=0 ; i<8; i++) {
            DLCircleListCollectionCellViewModel *viewModel = [[DLCircleListCollectionCellViewModel alloc] init];
            viewModel.headerImageStr = @"http://mmbiz.qpic.cn/mmbiz/XxE4icZUMxeFjluqQcibibdvEfUyYBgrQ3k7kdSMEB3vRwvjGecrPUPpHW0qZS21NFdOASOajiawm6vfKEZoyFoUVQ/640?wx_fmt=jpeg&wxfrom=5";
            viewModel.name = @"我是一只鹦鹉鱼";
            viewModel.articleNum = @"1568";
            viewModel.peopleNum = @"568";
            viewModel.topicNum = @"5749";
            viewModel.content = @"学点新东西,貌似还不错";
            [reArray addObject:viewModel];
        }
        self.dataArray = reArray;
        
        //
        [self.listHeaderViewModel.refreshUISubject subscribeNext:^(id  _Nullable x) {
            
        }];
        [self.refreshEndSubject sendNext:@(LSFooterRefresh_HasMoreData)];
        DismissHud();
        
    }];
    
    // 监控 命令的执行过程
    [[[self.refreshDataCommand.executing skip:1] take:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x isEqualToNumber:@(YES)]) {
            
            ShowMaskStatus(@"正在加载");
        }
    }];
    
    //
    [self.nextPageCommad.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        NSMutableArray *reArray = [[NSMutableArray alloc] initWithArray:self.dataArray];
        for (int i = 0; i < 8; i++) {
            
            DLCircleListCollectionCellViewModel *viewModel = [[DLCircleListCollectionCellViewModel alloc] init];
            viewModel.headerImageStr = @"http://mmbiz.qpic.cn/mmbiz/XxE4icZUMxeFjluqQcibibdvEfUyYBgrQ3k7kdSMEB3vRwvjGecrPUPpHW0qZS21NFdOASOajiawm6vfKEZoyFoUVQ/640?wx_fmt=jpeg&wxfrom=5";
            viewModel.name = @"我是一只鹦鹉鱼";
            viewModel.articleNum = @"1568";
            viewModel.peopleNum = @"568";
            viewModel.topicNum = @"5749";
            viewModel.content = @"自己交保险是不是只能交养老和医疗，费用是多少?";
            [reArray addObject:viewModel];
        }
        
        self.dataArray = reArray;
        [self.refreshEndSubject sendNext:@(LSFooterRefresh_HasMoreData)];
        DismissHud();
    }];
    
}



#pragma mark ---- Geter
- (RACSubject *)refreshUI
{
    if (!_refreshUI) {
        _refreshUI = [RACSubject subject];
    }
    return _refreshUI;
}

- (RACSubject *)refreshEndSubject
{
    if (!_refreshEndSubject) {
        _refreshEndSubject = [RACSubject subject];
    }
    return _refreshEndSubject;
}

- (RACCommand *)refreshDataCommand
{
    // 模拟 请求数据 刷新界面的command
    if (!_refreshDataCommand) {
        
        @weakify(self)
        _refreshDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self)
            return  [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                @strongify(self)
                self.currentPage = 1;
                [self.request POST:REQUEST_URL parameters:nil success:^(CMRequest *request, NSString *responseString) {
                    // 实际是从 网络数据请求 是
                    //                    NSDictionary *dict = [responseString objectfrom:responseString]
                    NSDictionary *dic = [NSDictionary dictionary];
                    [subscriber sendNext:dic];
                    [subscriber sendCompleted];
                    
                } failure:^(CMRequest *request, NSError *error) {
                    ShowErrorStatus(@"网络连接失败");
                    [subscriber sendCompleted];
                }];
                return  nil;
            }];
        }];
    }
    return  _refreshDataCommand;
}

- (RACCommand *)nextPageCommad
{
    if (!_nextPageCommad) {
        @weakify(self)
        _nextPageCommad = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self)
            return  [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                @strongify(self)
                self.currentPage++;
                [self.request POST:REQUEST_URL parameters:nil success:^(CMRequest *request, NSString *responseString) {

                    // 实际是 模拟获取 jsonObject 从 String 中
//                  NSDictionary *dict = [responseString objectFromJSONString];
                    NSDictionary *dict = [NSDictionary dictionary];
                    [subscriber sendNext:dict];
                    [subscriber sendCompleted];
                    
                } failure:^(CMRequest *request, NSError *error) {
                    @strongify(self);
                    self.currentPage --;
                    ShowErrorStatus(@"网络连接失败");
                    [subscriber sendCompleted];
                }];
                return  nil;
            }];
        }];
        
    }
    return _nextPageCommad;
}


-  (DLCircleListHeaderViewModel *)listHeaderViewModel
{
    if (!_listHeaderViewModel) {
        _listHeaderViewModel = [[DLCircleListHeaderViewModel alloc] init];
        _listHeaderViewModel.title = @"已加入的社团?洪兴";
        _listHeaderViewModel.cellClickSubject = self.cellClickSubject;
    }
    return _listHeaderViewModel;
}


- (DLCircleListSectionHeaderViewModel *)sectionHeaderViewModel
{
    if (!_sectionHeaderViewModel) {
        _sectionHeaderViewModel = [[DLCircleListSectionHeaderViewModel alloc] init];
        _sectionHeaderViewModel.title = @"推荐的圈子?东星";
    }
    return _sectionHeaderViewModel;
}

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (RACSubject *)cellClickSubject
{
    if (!_cellClickSubject) {
        _cellClickSubject = [RACSubject subject];
    }
    return _cellClickSubject;
}












@end
